//package com.huawei.codearts.tools.aop;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.huawei.codearts.tools.utils.ResponseUtils;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//import org.springframework.web.multipart.support.MultipartFilter;
//
//import java.util.Arrays;
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Aspect
//@Component
//public class LogAspect {
//    /**
//     * Before切入点
//     */
//    @Pointcut("execution(* com.huawei.codearts.controller.MemberController.*(..))")
////    @Pointcut("execution(* com.huawei.codearts.service.*.*(..))") //标记切点规则
////    @Pointcut("@annotation(org.springframework.stereotype.Service)")
//    public void beforePointcut() {
//    }
//
//    /**
//     * Around切入点
//     */
////    @Pointcut("@annotation(org.springframework.stereotype.Service)")
////    @Pointcut("execution(public * com.huawei.codearts.service.*.*(..))")
//    @Pointcut("execution(* com.huawei.codearts.controller.*.*(..))")
//    public void aroundPointcut() {
//    }
//
//
//    /**
//     * 记录请求日志的切面
//     * @param joinPoint
//     */
//    @Before("beforePointcut()")  //前置通知  切点就是上面创建的beforePointcut()方法
//    public void doBefore(JoinPoint joinPoint) {
//        log.info("doBefore路径");
//        try {
//            addLog(joinPoint,"",0);
//        } catch (Exception e) {
//            log.warn("doBefore日志记录异常，异常信息为: {}", e.getMessage());
//        }
//    }
//
////    @AfterReturning(pointcut="aroundPointcut()",returning="a")  // 后置通知  returning是方法中定义的参数
////    public void doAfterReturning(Object a) {
////        System.out.println("注解后置通知"+a);
////    }
////
////    @After("aroundPointcut()")  // 最终通知
////    public void doAfter() {
////        System.out.println("注解最终通知");
////    }
//
//    /**
//     * 记录请求和响应日志的切面
//     * @param joinPoint
//     * @return
//     * @throws Throwable
//     */
//    @Around("aroundPointcut()")  // 环绕通知
//    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
//        Object[] args = joinPoint.getArgs();
//        Object result = null;
//        ObjectMapper mapper = new ObjectMapper();
//        try {
//            log.info("LogAspect doAround日志记录，start...");
//            long startTime = System.currentTimeMillis();
//            result = joinPoint.proceed(args);
//            long endTime = System.currentTimeMillis();
//            long time = endTime - startTime;
//            log.info("LogAspect doAround日志记录，end...");
//            addLog(joinPoint,mapper.writeValueAsString(result),time);
//        } catch (Exception e) {
//            log.error("LogAspect doAround日志记录异常，异常信息为: {}", e.getMessage());
//            log.error("Exception in {}.{}() with cause = '{}' and stackTrace = {}",
//                joinPoint.getSignature().getDeclaringTypeName(),
//                joinPoint.getSignature().getName(),
//                e.getCause() != null ? e.getCause() : "NULL",
//                e);
////            throw e;
//            return ResponseUtils.logException(e);
//        }
//        return result;
//    }
//
//    /**
//     * 日志记录入库操作
//     */
//    public void addLog(JoinPoint joinPoint, String outParams, long time) throws JsonProcessingException {
//        ObjectMapper mapper = new ObjectMapper();
//        HttpServletRequest request = ((ServletRequestAttributes)
//                Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
//        log.info("\n\r=======================================\n\r" +
//                        "请求地址:{} \n\r" +
//                        "请求方式:{} \n\r" +
//                        "请求类方法:{} \n\r" +
//                        "请求方法参数:{} \n\r" +
//                        "返回报文:{} \n\r" +
//                        "处理耗时:{} ms \n\r" +
//                        "=======================================\n\r",
//                request.getRequestURI(),
//                request.getMethod(),
//                joinPoint.getSignature(),
////                JSONUtil.toJsonStr(filterArgs(joinPoint.getArgs())),
//                mapper.writeValueAsString(filterArgs(joinPoint.getArgs())),
//                outParams,
//                String.valueOf(time)
//        );
//    }
//
//    /**
//     * 过滤参数
//     * @param args
//     * @return
//     */
//    private List<Object> filterArgs(Object[] args) {
//        return Arrays.stream(args).filter(object -> !(object instanceof MultipartFilter)
//                && !(object instanceof HttpServletRequest)
//                && !(object instanceof HttpServletResponse)
//        ).collect(Collectors.toList());
//    }
//
//}